using UnityEngine;

public class AIItemStarter : MonoBehaviour
{
	[HideInInspector]
	public CharacterSystem character;

	[HideInInspector]
	public AICharacterShooterNav ai;

	public ItemSet[] ItemSets;

	public float TimeWait = 60f;

	private float timeTmp;

	[HideInInspector]
	public int CurrentSet;

	private void Start()
	{
		character = GetComponent<CharacterSystem>();
		ai = character.GetComponent<AICharacterShooterNav>();
		timeTmp = Time.time;
		TimeWait = UnityEngine.Random.Range(TimeWait / 2f, TimeWait);
	}

	private void Update()
	{
		if (!character || !character.isServer || ItemSets.Length <= CurrentSet || !(Time.time >= timeTmp + TimeWait))
		{
			return;
		}
		if (getNewSet())
		{
			CurrentSet++;
			if ((bool)ai)
			{
				ai.Fighting = true;
			}
		}
		timeTmp = Time.time;
	}

	private bool getNewSet()
	{
		if (character != null && character.inventory != null)
		{
			for (int i = 0; i < ItemSets.Length; i++)
			{
				if (i != CurrentSet)
				{
					continue;
				}
				TimeWait = ItemSets[i].NextTime + (float)UnityEngine.Random.Range(0, 10);
				if (ItemSets[i].RandomEquipOne)
				{
					character.inventory.AddItemByItemData(ItemSets[i].Items[Random.Range(0, ItemSets[i].Items.Length)], 1, -1, -1);
					character.inventory.UpdateInventoryToAll();
				}
				else
				{
					for (int j = 0; j < ItemSets[i].Items.Length; j++)
					{
						character.inventory.AddItemByItemData(ItemSets[i].Items[j], 1, -1, -1);
					}
					character.inventory.UpdateInventoryToAll();
				}
				return true;
			}
		}
		return false;
	}
}
